package hr.exception;


import hr.utils.Result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常返回
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        e.printStackTrace();

        return R.error().setMessage("系统未知异常，稍后再试");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public R handleHttpMessageNotReadableException(Exception e) {
        e.printStackTrace();

        return R.error().setMessage("缺少必要的请求参数");
    }

    @ExceptionHandler(CustomException.class)
    public R handleCustomException(Exception e) {
        e.printStackTrace();

        return R.error().setMessage(e.getMessage());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R handleHttpRequestMethodNotSupportedException(Exception e) {
        e.printStackTrace();

        return R.error().setMessage("不受支持的请求方式");
    }

    @ExceptionHandler(io.jsonwebtoken.SignatureException.class)
    public R handleSignatureException(Exception e) {
        e.printStackTrace();

        return R.error().setMessage("没有访问权限！");
    }

    @ExceptionHandler(org.springframework.web.HttpMediaTypeNotSupportedException.class)
    public R handleHttpMediaTypeNotSupportedException(Exception e) {
        e.printStackTrace();

        return R.error().setMessage("不受支持的请求数据类型！" + e.getMessage().replace("is not supported", ""));
    }
}
